import jwt
from django.conf import settings
from django.http import JsonResponse
from django.shortcuts import render
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.parsers import JSONParser

from table.models import project_model
from table.parmaCheck import table_add_sql, table_update_sql, table_delete_sql


# Create your views here.
def code(db):
    token = jwt.encode({
        "case_name": db.case_name,
        "user_id": db.user_id
    }, settings.SECRET_KEY, algorithm='HS256')
    return  token

@api_view(['POST'])
def table_add(request):
    data=request.data
    for i in table_add_sql:
        if not request.data[i]:
            return  JsonResponse({"message":"参数不能为空"},status=status.HTTP_400_BAD_REQUEST)

    case_name=data['case_name']
    user_id=data['user_id']
    priority=data['priority']
    pre_condiction=data['pre_condiction']
    plan_id=data['plan_id']
    case_type=data['case_type']
    review_status=data['review_status']
    suggestion=data['suggestion']

    dbNmae=project_model.objects.filter(case_name=case_name)

    if   dbNmae.exists():
        return  JsonResponse({"message": "已存在此条数据"}, status=status.HTTP_400_BAD_REQUEST)

    dbRes = project_model.objects.create(case_name=case_name, user_id=user_id, priority=priority,
                                             pre_condiction=pre_condiction, plan_id=plan_id, case_type=case_type,
                                             review_status=review_status, suggestion=suggestion)
    if case_name==dbRes.case_name:
        token = code(dbRes)
        return JsonResponse({"message": "添加成功","Token":token}, status=status.HTTP_200_OK)




@api_view(['POST'])
def table_update(request):
    data=request.data
    for i in table_update_sql:
        if not data[i]:
            return JsonResponse({"message": "参数不能为空"}, status=status.HTTP_400_BAD_REQUEST)

    id=data['id']
    case_name=data['case_name']
    priority=data['priority']
    suggestion=data['suggestion']
    dbID=project_model.objects.filter(id=id)
    if not dbID.exists():
        return JsonResponse({"message": "id不存在"}, status=status.HTTP_404_NOT_FOUND)
    try:
        dbID.update(case_name=case_name,priority=priority,suggestion=suggestion)
        return  JsonResponse({"message": "修改成功"}, status=status.HTTP_200_OK)
    except Exception:
        return JsonResponse({"message": "修改失败"}, status=status.HTTP_400_BAD_REQUEST)



@api_view(['POST'])
def table_delete(request):
    data=request.data
    for i in table_delete_sql:
        if not data[i]:
            return JsonResponse({"message": "参数不能为空"}, status=status.HTTP_400_BAD_REQUEST)

    id=data['id']
    dbID=project_model.objects.filter(id=id)
    if not dbID.exists():
        return JsonResponse({"message": "id不存在"}, status=status.HTTP_404_NOT_FOUND)
    try:
        dbID.delete()
        return  JsonResponse({"message": "删除成功"}, status=status.HTTP_200_OK)
    except Exception:
        return JsonResponse({"message": "删除失败"}, status=status.HTTP_400_BAD_REQUEST)





